package com.imoblife.brainwavestus;

import java.util.ArrayList;

import com.imoblife.brainwavestus.adapter.DiscountProductAdapter;
import com.imoblife.brainwavestus.adapter.LvBaseProductAdapter;
import com.imoblife.brainwavestus.customview.MenuList;
import com.imoblife.brainwavestus.listener.OnProductItemClickListener;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.obpurchaseSystem.OBpurchaseSystem;
import com.imoblife.brainwavestus.obpurchaseSystem.TusPointManager;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.DBUtil;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class GroupCommonProductActivity extends BaseActivity {
	
	private Context mCtx;
	private ListView mLvCommonProduct;
	private int source = Constant.SOURCE_FROM_LOCAL;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		mCtx = this;
		
		if (LaunchScreenActivity.IS_PAD) {
			
			setContentView(R.layout.pad_common_product_layout);
		} else {
			
			setContentView(R.layout.phone_common_product_layout);
		}
		
		init();
	}

	private void init() {
		
		int source = getIntent().getIntExtra(Constant.SOURCE, Constant.SOURCE_FROM_STORE);
		
		switch(source) {
			
		case Constant.SOURCE_FROM_LOCAL:
			initTitleName(getString(R.string.library));
			break;
		case Constant.SOURCE_FROM_STORE:
			source = Constant.SOURCE_FROM_STORE;
			initTitleName(getString(R.string.store));
			break;
		}
		
		mLvCommonProduct = (ListView) findViewById(R.id.lv_common_product);
		
		Store store = getBrainWavesApplication().getStore();
		
		ArrayList<Product> commonProducts = null;
		
		String action = getIntent().getAction();	
		LvBaseProductAdapter adapter = null;
		
		if (action.equals(
				Constant.ACTION_SALE_GROUP_NEW)) {
			
			commonProducts = 
					store.getSaleGroupProducts(Constant.GROUP_NEW);
			adapter = new LvBaseProductAdapter(this, 
					R.layout.item_common_lv_product_layout);
			
		} else if(action.equals(Constant.ACTION_SALE_GROUP_DISCOUNT)){
			
			TextView title = (TextView)findViewById(R.id.title);
			title.setText(R.string.specials);
			commonProducts = 
					store.getSaleGroupProducts(Constant.GROUP_DISCOUNT);
			adapter = new DiscountProductAdapter(this, 
					R.layout.item_lv_discount_product_layout);
		} else if(action.equals(Constant.ACTION_CATEGORY_SHOW)) {
			
			TextView title = (TextView)findViewById(R.id.title);
			Intent intent = getIntent();
			
			String categoryName = intent.getStringExtra(Constant.CATEGORY_NAME);
			
			title.setText(categoryName);
			
			DebugUtil.storeDebug(TAG, "categoryName:" + categoryName);
			
			User user = getBrainWavesApplication().getUser();
			
			switch(source) {
			
			case Constant.SOURCE_FROM_LOCAL:
				commonProducts = 
						user.getLocalLib().getProductByCategory(categoryName, store);
				
				break;
			case Constant.SOURCE_FROM_STORE:
				commonProducts = 
						store.getProductByCategory(categoryName);
				break;
			}
						
			adapter = new LvBaseProductAdapter(this, 
					R.layout.item_common_lv_product_layout);
			adapter.setIsShowSaleGroupIcon(false);
		}
		
		if(adapter != null){
			
			adapter.setData(commonProducts).setRightIconId(R.drawable.enter_icon);			
			
			mLvCommonProduct.setAdapter(adapter);			
			mLvCommonProduct
				.setOnItemClickListener(
						new OnProductItemClickListener(
								mCtx, 
								commonProducts,
								source));
		}	
	}
	
	@Override
	protected void onStart() {
		
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
